<?php

namespace App\Models\User;

use App\Models\Common\CommonModel;
use App\Models\Rbac\GuardUser;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class ChengBao extends CommonModel
{
    use HasFactory;

    protected $table = 'cheng_bao_order';

    public function getGuardUser() {
        return $this->hasOne(GuardUser::class,'id','account')->withDefault(['name'=>'未知']);
    }

    public function getRecords(array $conditions) {
        $records  = $this->newQuery();
        if(!empty($conditions['organName'])){
            $records = $records->where('organ_name','like','%'.$conditions['organName'].'%');
        }
        if(!empty($conditions['beibaoName'])){
            $records = $records->where('beibao_name','like','%'.$conditions['beibaoName'].'%');
        }
        if(!empty($conditions['importNumber'])){
            $records = $records->where('import_number',$conditions['importNumber']);
        }
        if(!empty($conditions['caseNumber'])){
            $records = $records->where('case_number',$conditions['caseNumber']);
        }
        if(!empty($conditions['baoxianType'])){
            $records = $records->where('baoxian_type',$conditions['baoxianType']);
        }
        if(!empty($conditions['carNumber'])){
            $records = $records->where('car_number',$conditions['carNumber']);
        }
        if(!empty($conditions['useWay'])){
            $records = $records->where('use_way',$conditions['useWay']);
        }
        if(!empty($conditions['seat'])){
            $records = $records->where('seat',$conditions['seat']);
        }
        if(!empty($conditions['toubaoDate'])){
            $records = $records->whereBetween('toubao_date',$conditions['toubaoDate']);
        }

        if(!empty($conditions['serviceBuyDate'])){
            $records = $records->whereBetween('service_buy_date',$conditions['serviceBuyDate']);
        }
        if(!empty($conditions['baoxianSDate'])){
            $records = $records->whereBetween('baoxian_s_date',$conditions['baoxianSDate']);
        }
        if(!empty($conditions['baoxianEDate'])){
            $records = $records->whereBetween('baoxian_e_date',$conditions['baoxianEDate']);
        }
        if(!empty($conditions['createdAt'])){
            $records = $records->whereBetween('created_at',$conditions['createdAt']);
        }
        return $records->orderByDesc('id');
    }


}
